package com.szgr.baoyin.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.szgr.baoyin.MyApplication;
import com.szgr.baoyin.R;
import com.szgr.baoyin.model.CardType;
import com.szgr.baoyin.model.DYCard;
import com.szgr.baoyin.utils.Constant;
import com.szgr.baoyin.utils.DateUtils;
import com.szgr.baoyin.utils.MyTool;
import com.szgr.baoyin.utils.RLog;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DYCardDetailActivity extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_dycard);
        super.onCreate(savedInstanceState);
    }

    @Override
    public void initActionBar() {
        actionBarTitle.setText("都银卡详情");
    }

    @Override
    public void initView() {
        Intent intent = getIntent();
//        if(intent != null && intent.hasExtra("id")) {
//            getCardDetail(intent.getStringExtra("id"));
//        }

        TextView tv_name = (TextView) findViewById(R.id.card_name);
        TextView tv_number = (TextView) findViewById(R.id.card_number);
        TextView tv_number1 = (TextView) findViewById(R.id.card_number1);
        TextView tv_date = (TextView) findViewById(R.id.card_date);
        TextView tv_name1 = (TextView) findViewById(R.id.card_name1);
        TextView tv_level = (TextView) findViewById(R.id.card_level);
        TextView tv_money = (TextView) findViewById(R.id.card_money);
        TextView tv_discount1 = (TextView) findViewById(R.id.card_discount1);
        TextView tv_discount2 = (TextView) findViewById(R.id.card_discount2);

        if(intent != null && intent.hasExtra("card")){
            DYCard dyCard = (DYCard) intent.getSerializableExtra("card");
            CardType card = dyCard.getCard();

            tv_name.setText(card.getCard_name());
            tv_name1.setText(card.getCard_name());
            tv_number.setText(dyCard.getCard_sn());
            tv_number1.setText(dyCard.getCard_sn());
            tv_level.setText("Lv " + dyCard.getCard_id());
            tv_money.setText(dyCard.getCard_balance());

            tv_date.setText(DateUtils.paseDate(Long.parseLong(dyCard.getCard_add_time())) + " — " + DateUtils.paseDate(Long.parseLong(dyCard.getCard_end_time())));

            List<CardType.CardConfig> card_config = dyCard.getCard_config();
            for (CardType.CardConfig config : card_config) {
                if("12".equals(config.getStore_goods_class())){
                    tv_discount1.setText("配件 " + MyTool.formatDouble(Double.parseDouble(config.getCard_discount()) * 10) + "折");
                }else if("13".equals(config.getStore_goods_class())){
                    tv_discount2.setText("维保 " + MyTool.formatDouble(Double.parseDouble(config.getCard_discount()) * 10 ) + "折");
                }
            }
        }
    }

    private void getCardDetail(final String id) {
        StringRequest sr = new StringRequest(Request.Method.POST, Constant.URL_CARD_DETAIL,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String s) {
                        RLog.s("----detail--->" + s);
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {

            }
        }){
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String,String> map = new HashMap<>();
                map.put("key",MyApplication.getmInstance().getKey());
                map.put("id",id);
                return map;
            }
        };
        MyApplication.getmInstance().addToRequestQueue(sr);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.action_bar_left:
                finish();
                break;
        }
    }
}
